from flask import Flask
import config
from exts import db, mail, cache, csrf, avatars, jwt, cors
from flask_migrate import Migrate
from apps.front import front_bp
from apps.cmsapi import api_bp
from forum_celery import make_celery
from apps.media import media_bp
import commands
'''
flask db init  # 初始化数据库
flask db migrate  # 生成迁移文件
flask db upgrade  # 应用迁移
'''

app = Flask(__name__)
app.config.from_object(config)  # 加载配置


db.init_app(app)  # 初始化数据库
mail.init_app(app)
cache.init_app(app)
# pip install flask-wtf  token
csrf.init_app(app)
avatars.init_app(app)
jwt.init_app(app)
cors.init_app(app, resources={r"/cmsapi/*": {"origins": "*"}})

csrf.exempt(api_bp)

migrate = Migrate(app, db)  # 初始化迁移

'''
Python 操作redis安装包
pip install redis
pip install hiredis
运行命令： celery -A app.mycelery worker --loglevel=info -P gevent
'''
mycelery = make_celery(app)

# 注册蓝图
app.register_blueprint(front_bp)
app.register_blueprint(media_bp)
app.register_blueprint(api_bp)
# 注册命令
app.cli.command("init_boards")(commands.init_boards)
app.cli.command('init_developer')(commands.init_developor)
app.cli.command('role')(commands.init_roles)


if __name__ == '__main__':
    app.run(host='0.0.0.0')
